package org.dromara.system.service.impl;

import org.dromara.common.core.utils.DateUtils;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.system.domain.PayGroupAccount;
import org.springframework.stereotype.Service;
import org.dromara.system.domain.bo.PayBlackBo;
import org.dromara.system.domain.vo.PayBlackVo;
import org.dromara.system.domain.PayBlack;
import org.dromara.system.mapper.PayBlackMapper;
import org.dromara.system.service.IPayBlackService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * IP黑名单Service业务层处理
 *
 * @author Gavino
 * @date 2024-10-31
 */
@RequiredArgsConstructor
@Service
public class PayBlackServiceImpl implements IPayBlackService {

    private final PayBlackMapper baseMapper;

    /**
     * 查询IP黑名单
     *
     * @param id 主键
     * @return IP黑名单
     */
    @Override
    public PayBlackVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询IP黑名单列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return IP黑名单分页列表
     */
    @Override
    public TableDataInfo<PayBlackVo> queryPageList(PayBlackBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<PayBlack> lqw = buildQueryWrapper(bo);
        Page<PayBlackVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的IP黑名单列表
     *
     * @param bo 查询条件
     * @return IP黑名单列表
     */
    @Override
    public List<PayBlackVo> queryList(PayBlackBo bo) {
        LambdaQueryWrapper<PayBlack> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<PayBlack> buildQueryWrapper(PayBlackBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<PayBlack> lqw = Wrappers.lambdaQuery();
        if (LoginHelper.isAgent()) {
            lqw.eq(PayBlack::getCreateId, LoginHelper.getUserId());
        }
        lqw.eq(StringUtils.isNotBlank(bo.getVal()), PayBlack::getVal, bo.getVal());
        lqw.eq(bo.getType() != null, PayBlack::getType, bo.getType());
        lqw.like(StringUtils.isNotBlank(bo.getRemark()), PayBlack::getRemark, bo.getRemark());
        return lqw;
    }

    /**
     * 新增IP黑名单
     *
     * @param bo IP黑名单
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(PayBlackBo bo) {
        PayBlack add = MapstructUtils.convert(bo, PayBlack.class);
        add.setCreateId(LoginHelper.getUserId());
        add.setCreateName(LoginHelper.getUsername());
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改IP黑名单
     *
     * @param bo IP黑名单
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(PayBlackBo bo) {
        PayBlack update = MapstructUtils.convert(bo, PayBlack.class);
        update.setUpdateId(LoginHelper.getUserId());
        update.setUpdateName(LoginHelper.getUsername());
        update.setUpdateTime(DateUtils.getNowDate());
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 校验并批量删除IP黑名单信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        LambdaQueryWrapper<PayBlack> query = new LambdaQueryWrapper<>();
        query.in(PayBlack::getId, ids);
        if (LoginHelper.isAgent()) {
            query.eq(PayBlack::getCreateId, LoginHelper.getUserId());
        }
        return baseMapper.delete(query) > 0;
    }
}
